import { entityEffectClickCallback } from '@/service';
import { generateListLayerName } from '@/service/layer';
import { useSceneStore } from '@/store/scene';
import { retFilePath } from '@/utils';
import { nextTick } from 'vue';

export const addParticleEffect = (selEffect: any) => {
  const sceneStore = useSceneStore();

  // 粒子特效添加
  function addParticle(position: number[]) {
    if (!selEffect.value) return;
    const newEntity = JSON.parse(selEffect.value.configJson);
    newEntity.name = Math.random().toString().split('.')[1];
    newEntity.views[0].name = newEntity.name;
    newEntity.views[0].positions = [position];
    for (const comp of newEntity.components) {
      comp.name += Math.random().toString().split('.')[1];
      comp.owner = newEntity.name;
    }
    const layerParam: any = {
      code: newEntity.name,
      show: true,
      json: newEntity,
      name: generateListLayerName('entity'),
      layerName: newEntity.name,
      desc: '',
      type: 'entity',
      actualType: 'gltfmodel',
      attr: {
        position,
        translation: [0, 0, 0],
        rotation: newEntity.views[0].rotation,
        scale: newEntity.views[0].scale,
        color: '',
      },
      effect: {
        dataType: '2', // 1静态数据2动态数据
        request: {
          method: 'GET',
          url: '',
          ticket: '',
          data: '',
          refrush: 0,
          isRefrush: false,
        },
        fieldDef: [],
        dataMapping: {},
        requestData: [],
        list: [], // 判定器列表
      },
    };
    sceneStore.layerList.unshift(layerParam);
    nextTick(() => {
      entityEffectClickCallback({
        code: 'effect',
        clickObject: {
          id: layerParam.layerName,
        },
      });
    });
  }

  return {
    addParticle,
  };
};
